package com.hhsjdesign.dynamicui.utils

import android.content.Context
import android.util.Log
import android.view.ViewGroup
import androidx.lifecycle.*
import com.hhsjdesign.dynamicui.core.property.ViewBaseProperty
import com.hhsjdesign.dynamicui.core.widgetdata.WidgetData
import com.hhsjdesign.dynamicui.core.property.widget.LinearLayoutVProperty
import com.hhsjdesign.dynamicui.core.widgetdata.LinearLayoutHWidgetData
import com.hhsjdesign.dynamicui.core.widgetdata.WidgetTypeConst
import com.hhsjdesign.dynamicui.core.widgetimpl.IWidget
import com.hhsjdesign.dynamicui.core.widgetimpl.RootLayout

/**
 * 管理一个页面所有的组件
 */
class WidgetManager//第一步：新建LifecycleRegister
//第二步：设置lifecycleRegister的各种状态
//第三步：通知给对应的观察者对象
private constructor(private var context: Context) : LifecycleOwner {

    private lateinit var pageWidget: IWidget
    private var lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)

    init {
        lifecycleRegistry.currentState = Lifecycle.State.CREATED
        lifecycle.addObserver(MyObserverDemo())
    }

    private fun init(pageId: String): IWidget {
        val parentLayoutWidgetData = LinearLayoutHWidgetData("main")
        parentLayoutWidgetData.type = WidgetTypeConst.LAYOUT_LINEAR_VERTICAL
        parentLayoutWidgetData.specProperty = LinearLayoutVProperty()
        parentLayoutWidgetData.widgetTreeId = pageId
        parentLayoutWidgetData.viewProperty = ViewBaseProperty().apply {
            size[0] = 0
            size[1] = -1
            size[2] = 0
            size[3] = -2
        }
        return RootLayout(context, parentLayoutWidgetData)
    }


    //统一的创建视图的类
    open fun makeWidget(widgetDataList: MutableList<WidgetData>, pageId: String) {
//        if (widgetDataList.isEmpty()) {
//            throw IllegalStateException("widgetDataList is Empty")
//        }
        pageWidget = init(pageId)
        (pageWidget as RootLayout).superMakeWidget(widgetDataList)
    }

    //更新组件
    open fun updateWidget(widgetDataList: MutableList<WidgetData>) {
        if (widgetDataList.isEmpty()) {
            return
        }
        pageWidget.canRemakeView = false
        (pageWidget as RootLayout).superMakeWidget(widgetDataList)
    }

    //移除widget
    fun removeWidget(widgetId: String): Boolean {
        return pageWidget.removeWidget(widgetId)
    }

    //获取组件
    fun retrieveWidget(widgetId: String): IWidget? {
        return pageWidget.retrieveWidget(widgetId)
    }

    //绑定到父布局中
    fun attachToParent(viewGroup: ViewGroup) {
        pageWidget?.attachToViewParent(viewGroup)
    }


    override fun getLifecycle(): Lifecycle {
        return lifecycleRegistry
    }

    fun loadData() {
        pageWidget.loadData()
    }


    inner class MyObserverDemo : LifecycleObserver {

        @OnLifecycleEvent(Lifecycle.Event.ON_START)
        fun performOnStart() {
            Log.d(TAG, "performOnStart ")
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
        fun performOnResume() {
            Log.d(TAG, "performOnResume ")
            pageWidget.onResume()
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
        fun performOnPause() {
            Log.d(TAG, "performOnPause ")
            pageWidget.onPause()
        }


        @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
        fun performOnStop() {
            Log.d(TAG, "performOnStop ")
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
        fun performOnDestroy() {
            Log.d(TAG, "performOnDestroy ")
            pageWidget.onDestroy()
        }

    }

    companion object {
        private const val TAG = "WidgetManager"

        @JvmStatic
        fun createWidgetManager(context: Context): WidgetManager {
            return WidgetManager(context)
        }
    }

}